if orders then


orders.limit_loaded = {}
orders.price_loaded = {}


function orders:LoadLimit()
	local ret
	local now = lib:tickseconds()
	local file = lib.path.orders .. "limit."
	for i = 0, 19 do
		if lib:exists(file .. i) then
			local transaction = lib:LoadIni(file .. i)
			if transaction
				and transaction.CLASSCODE
				and transaction.SECCODE
				and transaction.CLIENT_CODE
				and transaction.ACCOUNT
				and transaction.TRANS_ID
				and transaction.QUANTITY
				and transaction.PRICE
				and transaction.OPERATION
				and transaction.ex__
				and transaction.ex__.date
				and transaction.ex__.time
				and transaction.ex__.sl
				--and transaction.ex__.max_qty
				--and transaction.ex__.risk_limit
				--and transaction.ex__.risk
				and transaction.ex__.instrument
				and self:IsNewLimit(transaction)
				and tonumber(transaction.QUANTITY) > 0
				and transaction.ex__.date == lib:FormatDate()
				and now - lib:tickseconds(transaction.ex__.time) >= -1
				and now - lib:tickseconds(transaction.ex__.time) < lib.global.file_ttl
			then
				if not self.limit_loaded[transaction.TRANS_ID]
					and (transaction.CLIENT_CODE ~= lib:GetClientCode()
					or transaction.ACCOUNT ~= lib:GetAccount(transaction.CLASSCODE))
					--and (not self.limit_loaded[transaction.SECCODE]
					--or now - self.limit_loaded[transaction.SECCODE] > 3)
					and self:GetPosition(transaction.SECCODE, true) == 0
				then
					self.log("LoadLimits transaction OK", transaction)
					--message("LoadLimits transaction OK")
					local instrument = lib:GetInstrument(transaction.SECCODE)
					if instrument then
						self.log("LoadLimits instrument OK", instrument)
						local risk, max_qty = lib:CalcRisk(instrument, tonumber(transaction.PRICE), transaction.ex__.sl)
						if risk and max_qty then
							local qty = max_qty
							self.log("LoadLimits CalcRisk", risk, qty)
							
							if not transaction.ex__.risk then
								qty = 1
							else
								if risk > transaction.ex__.risk then
									qty = int(qty * transaction.ex__.risk / risk)
									--message(string.format("LoadLimit max risk %f risk %f max qty %d qty %d", risk, transaction.ex__.risk, max_qty, qty))
								end
							
								if transaction.ex__.credit and qty / max_qty > transaction.ex__.credit then
									qty = int(max_qty * transaction.ex__.credit)
									--message(string.format("LoadLimit credit %f max qty %d qty %d", transaction.ex__.credit, max_qty, qty))
								end
							end
							
							--local qty = int(max_lots / transaction.ex__.max_qty * tonumber(transaction.QUANTITY))
							if qty == 0 then qty = 1 end
							--qty = math.min(qty, max_lots)
							if qty > 0 then
								self.log("LoadLimits quantity to send", qty)
								for k, v in pairs(transaction.ex__.instrument) do
									 if string.match(k, "[^_]__$") then
										instrument[k] = v
									 end
								end
								instrument.qty__ = qty
								
								ret = self:SendLimit(instrument)
							end
						end
					end
				end
				self.limit_loaded[transaction.TRANS_ID] = now
				--self.limit_loaded[transaction.SECCODE] = now
			else
				--message("Remove limit file " .. i)
				lib:remove(file .. i)
			end
		end
		if ret then break end
	end
	
	for k, v in pairs(self.limit_loaded) do
		 if now - v > lib.global.file_ttl then
			self.limit_loaded[k] = nil
		 end
	end
	
	return ret
end
	
	
function orders:LoadPrice(name)
	if type(name) == "string" then
		local ret
		local now = lib:tickseconds()
		local file = lib.path.orders .. name .. "."
		for i = 0, 9 do
			local data = lib:LoadLabels(file .. i)
			if data then
				if now - int(data.seconds) >= -1 and now - int(data.seconds) < lib.global.file_ttl then
					if not self.price_loaded[data.id] then
						self.price_loaded[data.id] = int(data.seconds)
						ret = data
					end
				else
					lib:remove(file .. i)
				end
			end
			if ret then break end
		end
		for k, v in pairs(self.price_loaded) do
			 if now - v > lib.global.file_ttl then
				self.price_loaded[k] = nil
			 end
		end
		return ret
	end
end
	
	
function orders:LoadStop()
	local data = self:LoadPrice("stop")
	if data then
		self:SetStopPrice(data.sec_code, tonumber(data.price))
	end
	return data
end
	
	
function orders:LoadTake()
	local data = self:LoadPrice("take")
	if data then
		self:SetTakePrice(data.sec_code, tonumber(data.price))
	end
	return data
end


end -- of file
